﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Sinacor.Infra.Core.Licensing.Data
{
    public class Modulo : ResourceAccess
    {
        public int SelecionarProximoIdModulo()
        {
            int retorno = 0;

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                string sql = " SELECT MAX(NVL(COD_MOD_LOGC,0))+1 FROM TFWDMOD_LOGC ";
                

                command.CommandText = sql.ToString();
                retorno = Convert.ToInt32(this.ExecuteScalar(command));
            }

            return retorno;
        }

        public List<Sinacor.Infra.Core.Licensing.Module> Selecionar(Sinacor.Infra.Core.Licensing.Module library)
        {
            List<Sinacor.Infra.Core.Licensing.Module> response = null;

            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" SELECT COD_MOD_LOGC
                                        ,NOME_MOD_LOGC
                                        ,SIGL_MOD_LOGC                                          
                                       FROM TFWDMOD_LOGC
                                       WHERE (COD_MOD_LOGC      = :COD_MOD_LOGC      OR :COD_MOD_LOGC      IS NULL)
                                       AND   (NOME_MOD_LOGCULO  = :NOME_MOD_LOGCULO  OR :NOME_MOD_LOGCULO  IS NULL)
                                       AND   (SIGL_MOD_LOGC     = :SIGL_MOD_LOGC     OR :SIGL_MOD_LOGC     IS NULL) ");

                

                
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ID);
                this.AddInParameter(command, "NOME_MOD_LOGC", DbType.String, library.Name);
                this.AddInParameter(command, "SIGL_MOD_LOGC", DbType.String, library.Monogram);

                

                command.CommandText = sql.ToString();
                response = this.FindAll<Sinacor.Infra.Core.Licensing.Module>(command,
                    (
                
reader => new Sinacor.Infra.Core.Licensing.Module
{
    ID = reader.GetValue<int>("COD_MOD_LOGC"),
    Name = reader.GetValue<string>("NOME_MOD_LOGC"),
    Monogram = reader.GetValue<string>("SIGL_MOD_LOGC")
}
                
)
                );
            }

            return response;
        }

        public List<Sinacor.Infra.Core.Licensing.Module> Selecionar()
        {
            List<Sinacor.Infra.Core.Licensing.Module> response = new List<Sinacor.Infra.Core.Licensing.Module>();
            Module module = null;
            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                sql.AppendLine(@"	SELECT 
										COD_MOD_LOGC, NOME_MOD_LOGC, SIGL_MOD_LOGC
									FROM 
										TFWDMOD_LOGC 
									ORDER BY 
										COD_MOD_LOGC, NOME_MOD_LOGC, SIGL_MOD_LOGC");

                

                command.CommandText = sql.ToString();

                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        module = new Module();
                        module.ID = reader.GetValue<int>("COD_MOD_LOGC");
                        module.Name = reader.GetValue<string>("NOME_MOD_LOGC");
                        module.Monogram = reader.GetValue<string>("SIGL_MOD_LOGC");

                        response.Add(module);
                    }
                }
            }

            return response;
        }

        public void Incluir(Sinacor.Infra.Core.Licensing.Module library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                

                sql.AppendLine(@" INSERT INTO TFWDMOD_LOGC
                                          (COD_MOD_LOGC
                                          ,NOME_MOD_LOGC
                                          ,SIGL_MOD_LOGC)
                                      VALUES
                                          (:COD_MOD_LOGC,
                                          ,:NOME_MOD_LOGCULO,
                                          ,:SIGL_MOD_LOGC)");
                

                
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ID);
                this.AddInParameter(command, "NOME_MOD_LOGC", DbType.String, library.Name);
                this.AddInParameter(command, "SIGL_MOD_LOGC", DbType.String, library.Monogram);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void Alterar(Sinacor.Infra.Core.Licensing.Module library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" UPDATE MODULO SET 
                                        ,NOME_MOD_LOGC = :NOME_MOD_LOGC
                                        ,SIGL_MOD_LOGC = :SIGL_MOD_LOGC
                                  WHERE  COD_MOD_LOGC  = :COD_MOD_LOGC ");
                

                
                this.AddInParameter(command, "NOME_MOD_LOGC", DbType.String, library.Name);
                this.AddInParameter(command, "SIGL_MOD_LOGC", DbType.String, library.Monogram);

                

                
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ID);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void Excluir(Sinacor.Infra.Core.Licensing.Module library)
        {
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                string sql = " DELETE FROM TFWDMOD_LOGC WHERE COD_MOD_LOGC = :COD_MOD_LOGC ";
                

                
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ID);
                

                command.CommandText = sql.ToString();
                this.ExecuteNonQuery(command);
            }
        }
    }
}